﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UI_Manager : MonoBehaviour
{
    public Text hpText;
    public Text androidCountText;

    public GameObject MenuWindow;
    public Button btn_m_Restart;
    public Button btn_m_Exit;
    public Button btn_m_Close;

    public GameObject FinishWindow;
    public Button btn_f_Restart;
    public Button btn_f_Exit;
    public Text t_F_Info;


    // Start is called before the first frame update
    void Awake()
    {
        GameInfo.Instance.onHpChange += UpdateHP;
        GameInfo.Instance.onHpChange += FinishGame_Lose;
        GameInfo.Instance.onEnemyChange += UpdateAndroidCount;
        GameInfo.Instance.onEnemyChange += FinishGame_Win;
    }
    private void Start()
    {
        Cursor.visible = false;
        btn_m_Restart.onClick.AddListener(OnClickRestart);
        btn_f_Restart.onClick.AddListener(OnClickRestart);
        btn_m_Exit.onClick.AddListener(OnClickExit);
        btn_f_Exit.onClick.AddListener(OnClickExit);
        btn_m_Close.onClick.AddListener(OnClickBtn_m_Closes);
    }
    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.visible = true;
            MenuWindow.SetActive(true);
            Time.timeScale = 0;
        }
    }
    void UpdateHP(float hp)
    {
        hp = hp*100 / GameInfo.Instance.GetPlayerFullBlood();
        if (hp <= 0) hp = 0;
        string value = hp.ToString("0.0");
        hpText.text = value + "%";
    }
    void FinishGame_Lose(float hp)
    {
        hp = hp * 100 / GameInfo.Instance.GetPlayerFullBlood();
        if (hp <= 0)
        {
            Cursor.visible = true;
            FinishWindow.SetActive(true);
            Time.timeScale = 0;
            t_F_Info.text = "失败乃兵家常事，少侠可重新来过!";
        }
    }
    void FinishGame_Win(int e)
    {
        if(e<=0)
        {
            Cursor.visible = true;
            FinishWindow.SetActive(true);
            Time.timeScale = 0;
            t_F_Info.text = "恭喜你，胜利了！！！";
        }
    }
    void UpdateAndroidCount(int count)
    {
        androidCountText.text = count.ToString();
    }
    void OnClickRestart()
    {
        //Todo..
        SceneManager.LoadScene("Loading");
    }
    void OnClickExit()
    {
        Application.Quit();
    }
    void OnClickBtn_m_Closes()
    {
        Cursor.visible = false;
        MenuWindow.SetActive(false);
        Time.timeScale = 1;
    }
}
